<?php

/**
 * Application model for Cake.
 *
 * This file is application-wide model file. You can put all
 * application-wide model-related methods here.
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       cake
 * @subpackage    cake.cake.libs.model
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

/**
 * Application model for Cake.
 *
 * This is a placeholder class.
 * Create the same file in app/app_model.php
 * Add your application-wide methods to the class, your models will inherit them.
 *
 * @package       cake
 * @subpackage    cake.cake.libs.model
 */
class AppModel extends Model
{

    public $useDbConfig = 'mongo';

    /**
     *  Checks for uniqueness with a combination of fields. For some reason the first
     * label has to be a valid of one of the fields in the form or the rule never triggers
     * 
     * 
     *      public $validate = array(
     *          'l_name' => array(
     *           'rule' => 'multiFieldUnique', 'fields' => array('l_name', 's_name'),
     *           'message' => 'Unit can only be used once'
     *           )
     *       );
     * 
     * 
     * @param array $check
     * @param array $params
     * @return boolean 
     */
    public function multiFieldUnique($check, $params)
    {
        $and = array();
        foreach ($params['fields'] as $field)
        {
            $and[] = array($field => $this->data[$this->name][$field]);
        }

        $q_params = array(
            'conditions' => array('$and' => $and)
        );

        $result = $this->find('all', $q_params);

        if (count($result) > 0)
        {
            foreach ($params['fields'] as $field)
            {
                $this->validationErrors[$field] = $params['message'];
            }
            return false;
        }

        return true;
    }

}
